package com.idlecode.panel.tool.cron;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.components.JBCheckBox;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-11-07 14:52
 **/
public class MinutePanel extends JPanel {

    private JBCheckBox cb_00 = new JBCheckBox("00");
    private JBCheckBox cb_01 = new JBCheckBox("01");
    private JBCheckBox cb_02 = new JBCheckBox("02");
    private JBCheckBox cb_03 = new JBCheckBox("03");
    private JBCheckBox cb_04 = new JBCheckBox("04");
    private JBCheckBox cb_05 = new JBCheckBox("05");
    private JBCheckBox cb_06 = new JBCheckBox("06");
    private JBCheckBox cb_07 = new JBCheckBox("07");
    private JBCheckBox cb_08 = new JBCheckBox("08");
    private JBCheckBox cb_09 = new JBCheckBox("09");
    private JBCheckBox cb_10 = new JBCheckBox("10");
    private JBCheckBox cb_11 = new JBCheckBox("11");
    private JBCheckBox cb_12 = new JBCheckBox("12");
    private JBCheckBox cb_13 = new JBCheckBox("13");
    private JBCheckBox cb_14 = new JBCheckBox("14");
    private JBCheckBox cb_15 = new JBCheckBox("15");
    private JBCheckBox cb_16 = new JBCheckBox("16");
    private JBCheckBox cb_17 = new JBCheckBox("17");
    private JBCheckBox cb_18 = new JBCheckBox("18");
    private JBCheckBox cb_19 = new JBCheckBox("19");
    private JBCheckBox cb_20 = new JBCheckBox("20");
    private JBCheckBox cb_21 = new JBCheckBox("21");
    private JBCheckBox cb_22 = new JBCheckBox("22");
    private JBCheckBox cb_23 = new JBCheckBox("23");
    private JBCheckBox cb_24 = new JBCheckBox("24");
    private JBCheckBox cb_25 = new JBCheckBox("25");
    private JBCheckBox cb_26 = new JBCheckBox("26");
    private JBCheckBox cb_27 = new JBCheckBox("27");
    private JBCheckBox cb_28 = new JBCheckBox("28");
    private JBCheckBox cb_29 = new JBCheckBox("29");
    private JBCheckBox cb_30 = new JBCheckBox("30");
    private JBCheckBox cb_31 = new JBCheckBox("31");
    private JBCheckBox cb_32 = new JBCheckBox("32");
    private JBCheckBox cb_33 = new JBCheckBox("33");
    private JBCheckBox cb_34 = new JBCheckBox("34");
    private JBCheckBox cb_35 = new JBCheckBox("35");
    private JBCheckBox cb_36 = new JBCheckBox("36");
    private JBCheckBox cb_37 = new JBCheckBox("37");
    private JBCheckBox cb_38 = new JBCheckBox("38");
    private JBCheckBox cb_39 = new JBCheckBox("39");
    private JBCheckBox cb_40 = new JBCheckBox("40");
    private JBCheckBox cb_41 = new JBCheckBox("41");
    private JBCheckBox cb_42 = new JBCheckBox("42");
    private JBCheckBox cb_43 = new JBCheckBox("43");
    private JBCheckBox cb_44 = new JBCheckBox("44");
    private JBCheckBox cb_45 = new JBCheckBox("45");
    private JBCheckBox cb_46 = new JBCheckBox("46");
    private JBCheckBox cb_47 = new JBCheckBox("47");
    private JBCheckBox cb_48 = new JBCheckBox("48");
    private JBCheckBox cb_49 = new JBCheckBox("49");
    private JBCheckBox cb_50 = new JBCheckBox("50");
    private JBCheckBox cb_51 = new JBCheckBox("51");
    private JBCheckBox cb_52 = new JBCheckBox("52");
    private JBCheckBox cb_53 = new JBCheckBox("53");
    private JBCheckBox cb_54 = new JBCheckBox("54");
    private JBCheckBox cb_55 = new JBCheckBox("55");
    private JBCheckBox cb_56 = new JBCheckBox("56");
    private JBCheckBox cb_57 = new JBCheckBox("57");
    private JBCheckBox cb_58 = new JBCheckBox("58");
    private JBCheckBox cb_59 = new JBCheckBox("59");

    private JTextField minuteTField;

    private List<JBCheckBox> group = new ArrayList<>();

    private JRadioButton perMinuteRadioBtn; //每分钟

    private JRadioButton cycleRadioBtn;//周期
    private ComboBox<String> cycleStartComboBox;
    private ComboBox<String> cycleEndComboBox;

    private JRadioButton circulateRadioBtn;//循环
    private ComboBox<String> circulateStartComboBox;
    private ComboBox<String> circulateIntervalComboBox;

    private JRadioButton designateRadioBtn;//指定

    public MinutePanel(JTextField minuteTField) {
        this.minuteTField = minuteTField;
        minuteTField.setText("*");
        //初始化组件
        initComponent();
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //每分钟
        JPanel perMinutePanel = new JPanel();
        perMinutePanel.setLayout(new BoxLayout(perMinutePanel, BoxLayout.X_AXIS));
        perMinuteRadioBtn = new JRadioButton("分钟", true);
        perMinuteRadioBtn.setFont(perMinuteRadioBtn.getFont().deriveFont(Font.BOLD, 13));
        perMinutePanel.add(perMinuteRadioBtn);
        perMinutePanel.add(Box.createRigidArea(new Dimension(5, 0)));
        perMinutePanel.add(new JLabel("允许的通配符[, - * /]"));
        perMinutePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(perMinutePanel);

        perMinuteRadioBtn.addActionListener(e -> {
            if (perMinuteRadioBtn.isSelected()) {
                minuteTField.setText("*");
            }
        });
        //周期
        JPanel cyclePanel = new JPanel();
        cyclePanel.setLayout(new BoxLayout(cyclePanel, BoxLayout.X_AXIS));
        cycleRadioBtn = new JRadioButton("周期", false);
        cycleRadioBtn.setFont(cycleRadioBtn.getFont().deriveFont(Font.BOLD, 13));
        cyclePanel.add(cycleRadioBtn);
        cyclePanel.add(Box.createRigidArea(new Dimension(5, 0)));

        cyclePanel.add(new JLabel("从"));

        String[] cycleStartOptions = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
                "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58"};
        cycleStartComboBox = new ComboBox<>(cycleStartOptions);
        cycleStartComboBox.setMaximumSize(new Dimension(50, 30));
        cyclePanel.add(cycleStartComboBox);
        cycleStartComboBox.addActionListener(e -> {
            if (!cycleRadioBtn.isSelected()) {
                cycleRadioBtn.setSelected(true);
            }
            minuteTField.setText(cycleStartComboBox.getSelectedItem() + "-" + cycleEndComboBox.getSelectedItem());
        });

        cyclePanel.add(new JLabel("-"));

        String[] cycleEndOptions = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
                "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
        cycleEndComboBox = new ComboBox<>(cycleEndOptions);
        cycleEndComboBox.setMaximumSize(new Dimension(50, 30));
        cyclePanel.add(cycleEndComboBox);
        cycleEndComboBox.addActionListener(e -> {
            if (!cycleRadioBtn.isSelected()) {
                cycleRadioBtn.setSelected(true);
            }
            minuteTField.setText(cycleStartComboBox.getSelectedItem() + "-" + cycleEndComboBox.getSelectedItem());
        });

        cyclePanel.add(new JLabel("分钟"));

        cyclePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(cyclePanel);

        cycleRadioBtn.addActionListener(e -> {
            if (cycleRadioBtn.isSelected()) {
                minuteTField.setText(cycleStartComboBox.getSelectedItem() + "-" + cycleEndComboBox.getSelectedItem());
            }
        });
        //循环
        JPanel circulatePanel = new JPanel();
        circulatePanel.setLayout(new BoxLayout(circulatePanel, BoxLayout.X_AXIS));
        circulateRadioBtn = new JRadioButton("", false);
        circulatePanel.add(circulateRadioBtn);

        circulatePanel.add(new JLabel("从"));

        String[] circulateStartOptions = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
                "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
        circulateStartComboBox = new ComboBox<>(circulateStartOptions);
        circulateStartComboBox.setMaximumSize(new Dimension(50, 30));
        circulatePanel.add(circulateStartComboBox);
        circulateStartComboBox.addActionListener(e -> {
            if (!circulateRadioBtn.isSelected()) {
                circulateRadioBtn.setSelected(true);
            }
            minuteTField.setText(circulateStartComboBox.getSelectedItem() + "/" + circulateIntervalComboBox.getSelectedItem());
        });

        circulatePanel.add(new JLabel("分钟开始, 每"));

        String[] circulateIntervalOptions = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
                "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
        circulateIntervalComboBox = new ComboBox<>(circulateIntervalOptions);
        circulateIntervalComboBox.setMaximumSize(new Dimension(50, 30));
        circulatePanel.add(circulateIntervalComboBox);
        circulateIntervalComboBox.addActionListener(e -> {
            if (!circulateRadioBtn.isSelected()) {
                circulateRadioBtn.setSelected(true);
            }
            minuteTField.setText(circulateStartComboBox.getSelectedItem() + "/" + circulateIntervalComboBox.getSelectedItem());
        });

        circulatePanel.add(new JLabel("分钟执行一次"));

        circulatePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(circulatePanel);

        circulateRadioBtn.addActionListener(e -> {
            if (circulateRadioBtn.isSelected()) {
                minuteTField.setText(circulateStartComboBox.getSelectedItem() + "/" + circulateIntervalComboBox.getSelectedItem());
            }
        });
        //指定
        JPanel designatePanel = new JPanel();
        designatePanel.setLayout(new BoxLayout(designatePanel, BoxLayout.X_AXIS));
        designateRadioBtn = new JRadioButton("指定", false);
        designateRadioBtn.setFont(designateRadioBtn.getFont().deriveFont(Font.BOLD, 13));
        designatePanel.add(designateRadioBtn);
        designatePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(designatePanel);

        designateRadioBtn.addActionListener(e -> {
            if (designateRadioBtn.isSelected()) {
                minuteTField.setText(getDesignateModeText());
            }
        });

        ButtonGroup group = new ButtonGroup();
        group.add(perMinuteRadioBtn);
        group.add(cycleRadioBtn);
        group.add(circulateRadioBtn);
        group.add(designateRadioBtn);


        /****************添加空白行*****************/
        this.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 10)));

        //多选按钮
        //第一行
        JPanel firstRowPanel = new JPanel();
        firstRowPanel.setLayout(new BoxLayout(firstRowPanel, BoxLayout.X_AXIS));
        firstRowPanel.add(cb_00);
        firstRowPanel.add(cb_01);
        firstRowPanel.add(cb_02);
        firstRowPanel.add(cb_03);
        firstRowPanel.add(cb_04);
        firstRowPanel.add(cb_05);
        firstRowPanel.add(cb_06);
        firstRowPanel.add(cb_07);
        firstRowPanel.add(cb_08);
        firstRowPanel.add(cb_09);
        this.add(firstRowPanel);
        //第二行
        JPanel secondRowPanel = new JPanel();
        secondRowPanel.setLayout(new BoxLayout(secondRowPanel, BoxLayout.X_AXIS));
        secondRowPanel.add(cb_10);
        secondRowPanel.add(cb_11);
        secondRowPanel.add(cb_12);
        secondRowPanel.add(cb_13);
        secondRowPanel.add(cb_14);
        secondRowPanel.add(cb_15);
        secondRowPanel.add(cb_16);
        secondRowPanel.add(cb_17);
        secondRowPanel.add(cb_18);
        secondRowPanel.add(cb_19);
        this.add(secondRowPanel);
        //第三行
        JPanel thirdRowPanel = new JPanel();
        thirdRowPanel.setLayout(new BoxLayout(thirdRowPanel, BoxLayout.X_AXIS));
        thirdRowPanel.add(cb_20);
        thirdRowPanel.add(cb_21);
        thirdRowPanel.add(cb_22);
        thirdRowPanel.add(cb_23);
        thirdRowPanel.add(cb_24);
        thirdRowPanel.add(cb_25);
        thirdRowPanel.add(cb_26);
        thirdRowPanel.add(cb_27);
        thirdRowPanel.add(cb_28);
        thirdRowPanel.add(cb_29);
        this.add(thirdRowPanel);
        //第四行
        JPanel fourthRowPanel = new JPanel();
        fourthRowPanel.setLayout(new BoxLayout(fourthRowPanel, BoxLayout.X_AXIS));
        fourthRowPanel.add(cb_30);
        fourthRowPanel.add(cb_31);
        fourthRowPanel.add(cb_32);
        fourthRowPanel.add(cb_33);
        fourthRowPanel.add(cb_34);
        fourthRowPanel.add(cb_35);
        fourthRowPanel.add(cb_36);
        fourthRowPanel.add(cb_37);
        fourthRowPanel.add(cb_38);
        fourthRowPanel.add(cb_39);
        this.add(fourthRowPanel);
        //第五行
        JPanel fifthRowPanel = new JPanel();
        fifthRowPanel.setLayout(new BoxLayout(fifthRowPanel, BoxLayout.X_AXIS));
        fifthRowPanel.add(cb_40);
        fifthRowPanel.add(cb_41);
        fifthRowPanel.add(cb_42);
        fifthRowPanel.add(cb_43);
        fifthRowPanel.add(cb_44);
        fifthRowPanel.add(cb_45);
        fifthRowPanel.add(cb_46);
        fifthRowPanel.add(cb_47);
        fifthRowPanel.add(cb_48);
        fifthRowPanel.add(cb_49);
        this.add(fifthRowPanel);
        //第六行
        JPanel sixthRowPanel = new JPanel();
        sixthRowPanel.setLayout(new BoxLayout(sixthRowPanel, BoxLayout.X_AXIS));
        sixthRowPanel.add(cb_50);
        sixthRowPanel.add(cb_51);
        sixthRowPanel.add(cb_52);
        sixthRowPanel.add(cb_53);
        sixthRowPanel.add(cb_54);
        sixthRowPanel.add(cb_55);
        sixthRowPanel.add(cb_56);
        sixthRowPanel.add(cb_57);
        sixthRowPanel.add(cb_58);
        sixthRowPanel.add(cb_59);
        this.add(sixthRowPanel);
    }

    private void initComponent() {
        Collections.addAll(group,
                cb_00,
                cb_01,
                cb_02,
                cb_03,
                cb_04,
                cb_05,
                cb_06,
                cb_07,
                cb_08,
                cb_09,
                cb_10,
                cb_11,
                cb_12,
                cb_13,
                cb_14,
                cb_15,
                cb_16,
                cb_17,
                cb_18,
                cb_19,
                cb_20,
                cb_21,
                cb_22,
                cb_23,
                cb_24,
                cb_25,
                cb_26,
                cb_27,
                cb_28,
                cb_29,
                cb_30,
                cb_31,
                cb_32,
                cb_33,
                cb_34,
                cb_35,
                cb_36,
                cb_37,
                cb_38,
                cb_39,
                cb_40,
                cb_41,
                cb_42,
                cb_43,
                cb_44,
                cb_45,
                cb_46,
                cb_47,
                cb_48,
                cb_49,
                cb_50,
                cb_51,
                cb_52,
                cb_53,
                cb_54,
                cb_55,
                cb_56,
                cb_57,
                cb_58,
                cb_59);
        for (JBCheckBox jbCheckBox : group) {
            jbCheckBox.addItemListener(e -> {
                if (designateRadioBtn.isSelected()) {
                    String designateModeText = getDesignateModeText();
                    minuteTField.setText(designateModeText);
                }
            });
        }
    }

    private String getDesignateModeText() {
        List<Integer> list = new ArrayList<>();
        for (JBCheckBox jbCheckBox : group) {
            if (jbCheckBox.isSelected()) {
                list.add(Integer.valueOf(jbCheckBox.getText()));
            }
        }

        if (CollUtil.isEmpty(list)) {
            cb_00.setSelected(true);
            return Integer.valueOf(cb_00.getText()) + "";
        }
        //排序
        Collections.sort(list);

        return CollUtil.join(list, ",");
    }

    public void inverseParseUi(String expression) {
        if (StrUtil.isBlank(expression)) {
            return;
        }
        minuteTField.setText(expression);
        if ("*".equals(expression)) {
            perMinuteRadioBtn.setSelected(true);
            return;
        }
        if (expression.contains("-")) {
            cycleRadioBtn.setSelected(true);
            String[] cycleCondition = expression.split("-");
            for (int i = 0; i < cycleCondition.length; i++) {
                String condition = cycleCondition[i];
                switch (i) {
                    case 0:
                        cycleStartComboBox.setSelectedItem(condition);
                    case 1:
                        cycleEndComboBox.setSelectedItem(condition);
                    default:
                        break;
                }
            }
            return;
        }
        if (expression.contains("/")) {
            circulateRadioBtn.setSelected(true);
            String[] circulateCondition = expression.split("/");
            for (int i = 0; i < circulateCondition.length; i++) {
                String condition = circulateCondition[i];
                switch (i) {
                    case 0:
                        circulateStartComboBox.setSelectedItem(condition);
                    case 1:
                        circulateIntervalComboBox.setSelectedItem(condition);
                    default:
                        break;
                }
            }
            return;
        }

        designateRadioBtn.setSelected(true);

        String[] designateCondition = expression.split(",");

        for (int i = 0; i < group.size(); i++) {
            for (String condition : designateCondition) {
                if (condition.equals(i + "")) {
                    JBCheckBox jbCheckBox = group.get(i);
                    jbCheckBox.setSelected(true);
                }
            }
        }
    }
}
